/**
 * api2entity
 * <p>
 * Copyright 2014 Acooly.cn, Inc. All rights reserved.
 *
 * @author hesir
 * @date 2020-12-21 09:47
 */
package com.hesir.api2entity.services.qimen;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.hesir.api2entity.dto.QimenParamsDTO;
import com.hesir.api2entity.pojo.ApiEntity;
import com.hesir.api2entity.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author hesir
 * @date 2020-12-21 09:47
 */
@Service
@Slf4j
public class QimenProcessor {
    @Autowired
    ResourceLoader resourceLoader;

    Gson gson = new GsonBuilder().create();


    public void process(String pathStr,String folderPath,String type,String packagePath,String entityTitle){
        Resource resource = resourceLoader.getResource("classpath:./"+pathStr);
        StringBuffer sb = new StringBuffer();
        try {
            File file = new File(resource.getURI().getPath());
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            while ((text = bufferedReader.readLine()) != null) {
                sb.append(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String outStr = sb.toString();
        List<QimenParamsDTO> qimenParamsDTOList = gson.fromJson(outStr,new TypeToken<List<QimenParamsDTO>>(){}.getType());
        log.info("获取到的内容为：{}",gson.toJson(qimenParamsDTOList));
        createEntity(qimenParamsDTOList,folderPath,type,packagePath,entityTitle,"");

    }

    /**
     * 创建entity
     * @param qimenParamsDTOList
     */
    private  void  createEntity(List<QimenParamsDTO> qimenParamsDTOList,String folderPath,String type,String packagePath,String entityTitle,String entityDesc){
        List<ApiEntity> entityList = new ArrayList<>();
        String entityDescStr = "";
        for (QimenParamsDTO param : qimenParamsDTOList) {
            ApiEntity entity = new ApiEntity();
            if(param.getSubParams().size()>0){
                entity.setName(param.getName());
                entity.setRequired(param.getRequired());
                entity.setComment(param.getDescription());
                String subEntityTitle = "";
                if(param.getType().matches("(.*)\\[\\]")){
                    subEntityTitle = param.getType().replaceAll("[\\[|\\]]", "");
                    entity.setType("List<"+subEntityTitle+">");
                }else{
                    subEntityTitle = param.getType().replaceAll("[\\[|\\]]", "");
                    entity.setType(param.getType());
                }
                createEntity(param.getSubParams(),folderPath,type,packagePath,subEntityTitle,param.getDescription());
            }else{
                entity.setName(param.getName());
                entity.setRequired(param.getRequired());
                entity.setComment(param.getDescription());
                entity.setType(param.getType());
            }
            entityList.add(entity);
        }
        Boolean useCamel = false;
        FileUtils.createFile(entityTitle,entityDesc,entityList,folderPath,type,packagePath,useCamel);
    }
}
